package com.azunitech.jms;

import java.sql.Timestamp;
import java.util.Hashtable;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.junit.Test;

import com.azunitech.jms.common.EventMessage;
import com.azunitech.jms.common.domains.PartsEntity;

public class SendQueueTester {
	public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
	public final static String JMS_FACTORY="CF-COMMAND";
	public final static String QUEUE="Queue-1";
	public final static String URL = "t3://localhost:7001";
	java.util.Date date= new java.util.Date();
	
	@Test public void sendOneMsg() throws Exception{
		Hashtable<String, String> env = new Hashtable<String, String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
		env.put(Context.PROVIDER_URL, URL);
		InitialContext ctx =  new InitialContext(env);
		
		QueueConnectionFactory qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
		QueueConnection qcon = qconFactory.createQueueConnection();
		QueueSession qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		Queue queue = (Queue) ctx.lookup(QUEUE);
		QueueSender qsender = qsession.createSender(queue);
		qcon.start();
		
		for( int i = 0; i < 1; i++){
			BytesMessage bytesMessage = sendByteMessage(qsession);
			qsender.send(bytesMessage);
			
			TextMessage textMessage = sendTestMessage(qsession, i);
			qsender.send(textMessage);
			
			MapMessage mapMessage = sendMapMessage(qsession);
			qsender.send(mapMessage);
		
			StreamMessage streamMessage = sendStreamMessage(qsession);
			qsender.send(streamMessage);
			
			ObjectMessage eventMdg = sendEventMessage(qsession, i);
			qsender.send(eventMdg);
		
			ObjectMessage partMdg = sendPartMessage(qsession);
			qsender.send(partMdg);
			
			//Thread.sleep(10);
		}
		
		qsender.close();
		qsession.close();
		qcon.close();
	}
	
	private BytesMessage sendByteMessage( QueueSession qsession ) throws JMSException {
		BytesMessage bytesMessage = qsession.createBytesMessage();
		byte[] byteData = {-128, 127, -1, 0, 1, -64, 64};
        bytesMessage.writeBytes(byteData);
        return bytesMessage;
	}
	
	private TextMessage sendTestMessage( QueueSession qsession, int i ) throws JMSException{
		TextMessage textMessage = qsession.createTextMessage();
		textMessage.setText(new Timestamp(date.getTime()).toString() + " ------- " + i);
		return textMessage;
	}
	
	private MapMessage sendMapMessage( QueueSession qsession ) throws JMSException{
		MapMessage mapMessage = qsession.createMapMessage();
        mapMessage.setString("Message type", "Map");
        mapMessage.setInt("An Integer", 3456);
        mapMessage.setDouble("A Double", 1.23456789);
        return mapMessage;
	}
	
	private StreamMessage sendStreamMessage(QueueSession qsession ) throws JMSException{
		StreamMessage streamMessage = qsession.createStreamMessage();
        streamMessage.writeString("Stream message");
        streamMessage.writeDouble(123.456789e222);
        streamMessage.writeInt(223344);
        return streamMessage;
	}
	
	private ObjectMessage sendEventMessage(QueueSession qsession, int id ) throws JMSException{
		ObjectMessage objectMessage = qsession.createObjectMessage();
        objectMessage.setObject(new EventMessage(id, new Timestamp(date.getTime()).toString()));
        return objectMessage;
	}
	
	private ObjectMessage sendPartMessage(QueueSession qsession) throws JMSException{
		ObjectMessage objectMessage = qsession.createObjectMessage();
        objectMessage.setObject(new PartsEntity(new Timestamp(date.getTime()).toString()));
        return objectMessage;
	}
}
